package com.igroupes.ovineherd.model.attribute.address;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.AddressStyle;
import com.igroupes.ovineherd.model.Style.SwitchStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper=true)
public class AddressModelData extends BasicAttribute{
    private SwitchStyle priovince;
    private SwitchStyle city;
    private SwitchStyle county;

    public AddressModelData() {
        super();
        this.priovince = new SwitchStyle("记录省份", BooleanKey.TRUE.getCode(), false, "");
        this.city = new SwitchStyle("记录市", BooleanKey.TRUE.getCode(), false, "");
        this.county = new SwitchStyle("记录区", BooleanKey.TRUE.getCode(), false, "");
    }

    public Boolean verifyField(AddressModelData addressModelData) {
        super.verifyField(addressModelData);
        new SwitchStyle().verify(addressModelData.getPriovince());
        new SwitchStyle().verify(addressModelData.getCity());
        new SwitchStyle().verify(addressModelData.getCounty());
        return true;
    }
    public Boolean verifyFieldV2(AddressModelData basic) {
        super.verifyFieldV2(basic);
        new SwitchStyle().verify( new SwitchStyle("记录省份", basic.getPriovince().getValue(), false, ""));
        new SwitchStyle().verify(new SwitchStyle("记录市", basic.getCity().getValue(), false, ""));
        new SwitchStyle().verify(new SwitchStyle("记录区", basic.getCounty().getValue(), false, ""));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        AddressModelData addressModelData = (AddressModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setLength(8);
        String dbName = fieldQuery.getFieldName();
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        List<FieldQuery> fieldQueries1 = new ArrayList<>();

        fieldQuery.setType(MySqlTypeKey.BIGINT.getCode());
        fieldQuery.setFieldName(dbName);
        fieldQuery.setComment("");

        dataModelAttributeEntity.setDbName(dbName);
        dataModelAttributeEntity.setDbNumber(1);
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("city", Convert.toInt(addressModelData.getCity().getValue()));
        jsonObject.set("county", Convert.toInt(addressModelData.getCounty().getValue()));
        jsonObject.set("priovince", Convert.toInt(addressModelData.getPriovince().getValue()));
        jsonObject.set("dbName", dbName);
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries1.add(fieldQuery);
//
//        FieldQuery fieldQuery2 = new FieldQuery();
//        BeanUtil.copyProperties(fieldQuery, fieldQuery2);
//        fieldQuery2.setFieldName(dbName+"2");
//        DataModelAttributeEntity dataModelAttributeEntity1 = new DataModelAttributeEntity();
//        BeanUtil.copyProperties(dataModelAttributeEntity, dataModelAttributeEntity1);
//        dataModelAttributeEntity1.setDbName(dbName+"2");
//        fieldQuery2.setDataModelAttributeEntity(null);
//        fieldQueries1.add(fieldQuery2);
//
//        FieldQuery fieldQuery3 = new FieldQuery();
//        BeanUtil.copyProperties(fieldQuery, fieldQuery3);
//        fieldQuery3.setFieldName(dbName+"3");
//        DataModelAttributeEntity dataModelAttributeEntity2 = new DataModelAttributeEntity();
//        BeanUtil.copyProperties(dataModelAttributeEntity, dataModelAttributeEntity2);
//        dataModelAttributeEntity2.setDbName(dbName+"3");
//        fieldQuery3.setDataModelAttributeEntity(null);
//        fieldQueries1.add(fieldQuery3);
        return fieldQueries1;
    }



    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("priovince", new SwitchStyle("记录省份",modelDataJsonAttributeQuery.getJson().get("priovince"), false, ""));
        jsonObject.set("city", new SwitchStyle("记录市", modelDataJsonAttributeQuery.getJson().get("city"), false, ""));
        jsonObject.set("county", new SwitchStyle("记录区",modelDataJsonAttributeQuery.getJson().get("county"), false, ""));
        return jsonObject;
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldDescribes = new ArrayList<>();
        if(value == null){
            return fieldDescribes;
        }
//        JSONObject jsonObject = modelAttributeEntity.getJson();
//        JSONObject object = JSONUtil.parseObj(value);
//        if(object == null){
//            throw new ModelException(modelAttributeEntity.getName()+"传值类型错误，应该为一个对象");
//        }
//        if(jsonObject.get("priovince").equals(BooleanKey.FALSE.getCode())){
//            if(StringUtils.isBlank( object.get("priovince").toString())){
//                throw new ModelException(modelAttributeEntity.getName()+",priovince值不能为空");
//            }
//        }
//        if(jsonObject.get("city").equals(BooleanKey.FALSE.getCode())){
//            if(StringUtils.isBlank(object.get("city").toString())){
//                throw new ModelException(modelAttributeEntity.getName()+",city值不能为空");
//            }
//        }
//        if(jsonObject.get("county").equals(BooleanKey.FALSE.getCode())){
//            if(StringUtils.isBlank(object.get("county").toString())){
//                throw new ModelException(modelAttributeEntity.getName()+"，count值不能为空");
//            }
//        }
//        if(!StringUtils.isBlank((object.get("priovince").toString()))){
//            FieldApiResponse fieldApiResponse = new FieldApiResponse();
//            fieldApiResponse.setDbName(modelAttributeEntity.getDbName()+"1");
//            fieldApiResponse.setValue(object.get("priovince").toString());
//            fieldApiResponse.setIsStr(true);
//            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
//            fieldDescribes.add(fieldApiResponse);
//        }
//        if(!StringUtils.isBlank((object.get("city").toString()))){
//            FieldApiResponse fieldApiResponse = new FieldApiResponse();
//            fieldApiResponse.setDbName(modelAttributeEntity.getDbName()+"2");
//            fieldApiResponse.setValue(object.get("city").toString());
//            fieldApiResponse.setIsStr(true);
//            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
//            fieldDescribes.add(fieldApiResponse);
//        }
//        if(!StringUtils.isBlank(object.get("county").toString())){
//            FieldApiResponse fieldApiResponse = new FieldApiResponse();
//            fieldApiResponse.setDbName(modelAttributeEntity.getDbName()+"3");
//            fieldApiResponse.setValue(object.get("county").toString());
//            fieldApiResponse.setIsStr(true);
//            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
//            fieldDescribes.add(fieldApiResponse);
//        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(ObjectUtil.toString(JSONUtil.parseObj(value.toString())));
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldDescribes.add(fieldApiResponse);
        return fieldDescribes;
    }

    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
//        if(!jsonObject.get("priovince").equals(BooleanKey.FALSE.getCode())){
//            return null;
//        }
        return new JSONObject(new AddressStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
                jsonObject.get("city").equals(BooleanKey.TRUE.getCode()),
                jsonObject.get("county").equals(BooleanKey.TRUE.getCode())
        ), false);
    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.TEXT.getCode(), true, false, true);
    }
//    @Override
//    public List<JSONObject> getAttributeStyle(DataModelAttributeEntity dataModelAttributeEntity) {
//
//        DataJSON json = dataModelAttributeEntity.getJson().toBean(DataJSON.class);
//        Boolean readonly = super.getEditJurisdiction(type, dataModelAttributeEntity);
//        AddressStyle addressStyle = new AddressStyle(
//                dataModelAttributeEntity.getName(),
//                dataModelFormAttributeEntity != null?
//                        dataModelFormAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()):
//                        dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
//
//                org.apache.commons.lang.StringUtils.isBlank(dataModelAttributeEntity.getRemark()) ?
//                        "" :
//                        dataModelAttributeEntity.getRemark(), readonly,
//                dataModelAttributeEntity.getEditSpan());
//        if(json.getPriovince().equals(BooleanKey.TRUE.getCode())){
//            SelectStyle priovince = new SelectStyle("省",
//                    entity!= null ?
//                            (entity.getStr(dataModelAttributeEntity.getDbName()+"1") == null ?
//                                    "" :
//                                    entity.getStr(dataModelAttributeEntity.getDbName()+"1") ):
//                            "",
//                    new ArrayList<>(),"", SelectKey.MUTIL.getCode(), readonly);
//            addressStyle.setPriovince(priovince);
//        }
//        if(json.getCity().equals(BooleanKey.TRUE.getCode())){
//            SelectStyle city = new SelectStyle("市",
//                    entity!= null ?
//                            (entity.getStr(dataModelAttributeEntity.getDbName()+"2") == null ?
//                                    "" :
//                                    entity.getStr(dataModelAttributeEntity.getDbName()+"2")) :
//                            "",
//                    new ArrayList<>(),"", SelectKey.MUTIL.getCode(), readonly);
//            addressStyle.setCity(city);
//        }
//
//        if(json.getRegion().equals(BooleanKey.TRUE.getCode())){
//            SelectStyle region = new SelectStyle("区/县",
//                    entity!= null ?
//                            (entity.getStr(dataModelAttributeEntity.getDbName()+"3") == null ?
//                                    "" :
//                                    entity.getStr(dataModelAttributeEntity.getDbName()+"3")) :
//                            "",
//                    new ArrayList<>(),"", SelectKey.MUTIL.getCode(), readonly);
//            addressStyle.setRegion(region);
//        }
//        if(json.getInfo().equals(BooleanKey.TRUE.getCode())) {
//            TextStyle info = new TextStyle("地址详情",
//                    entity!= null ?
//                            (entity.getStr(dataModelAttributeEntity.getDbName()+"4") == null ?
//                                    "" :
//                                    entity.getStr(dataModelAttributeEntity.getDbName()+"4")) :
//                            "",
//                    dataModelFormAttributeEntity != null?
//                            dataModelFormAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()):
//                            dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
//                    100, "",
//                    "", "请输入" + dataModelAttributeEntity.getName(), readonly, 6);
//            addressStyle.setInfo(info);
//        }
//        return new JSONObject(addressStyle);
//    }

    @Override
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(ObjectUtil.isNotNull(value)){
            return JSONUtil.parseObj(value.toString());
        }
        return value;
    }
}
